﻿using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Enum;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DFGK_WMS.WebAPI.Controllers.PC.库存管理
{
    /// <summary>
    /// 库存流水
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class InventoryLogController : BaseController
    {
        private readonly IInventoryLogService inventoryLogService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="inventoryLogService"></param>
        public InventoryLogController(IWebHostEnvironment environment, IInventoryLogService inventoryLogService) : base(environment)
        {
            this.inventoryLogService = inventoryLogService;
        }
        /// <summary>
        /// 查询库存流水
        /// </summary>
        /// <param name="WarehouseId"></param>
        /// <param name="AreaId"></param>
        /// <param name="SourceType"></param>
        /// <param name="MaterialName"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInventoryflow(long WarehouseId, long AreaId, int SourceType, string? MaterialName, int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var list = inventoryLogService.GetInventoryflow(WarehouseId, AreaId, SourceType, MaterialName, pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));

            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 枚举库存流水类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetEmInventoryLogType()
        {
            var list = BaseGetEnum.GetCodeRulesList<EmInventoryLogType>();

            return Ok(list);
        }


        /// <summary>
        /// 出入库管理的的报表管理导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult InventoryMsgDown(int type)
        {
            var list = inventoryLogService.GetInventoryMsgDown(type);
            if (list != null)
                return InventoryMsgDown(list, type);
            else
                return Ok();
        }


        /// <summary>
        /// 获取库存快照
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult InventorySnapshot(DateTime? startDate, DateTime? endDate, int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var list = inventoryLogService.InventorySnapshot(startDate, endDate, out totalCount, pageIndex, pageSize);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));

            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
    }
}
